<?php

require_once 'Spreadsheet/Excel/Writer.php';

include 'DBLibrary/config.php';
include 'DBLibrary/openDB.php';

mysql_select_db($dbname);
// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();

// sending HTTP headers
$workbook = new Spreadsheet_Excel_Writer();

$rowedit = mysql_fetch_array(mysql_query("SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 8 HOUR),'%y-%m-%d-%h-%i-%s') as date"));

$workbook->send('Course-' . $rowedit['date'] . '.xls');

// Creating a worksheet
$worksheet = & $workbook->addWorksheet('Course List');

//format of text

$format_left = & $workbook->addFormat();
$format_left->setAlign('left');
$format_left->setAlign('middle');
$format_left->setFontFamily('Calibri');
$format_left->setSize('11');

$format_center = & $workbook->addFormat();
$format_center->setAlign('center');
$format_center->setAlign('middle');
$format_center->setFontFamily('Calibri');
$format_center->setSize('11');

$format_center_bold = & $workbook->addFormat();
$format_center_bold->setAlign('center');
$format_center_bold->setAlign('middle');
$format_center_bold->setBold('1');
$format_center_bold->setFontFamily('Calibri');
$format_center_bold->setSize('13');

/////////////////////////////////

$querycourse = "select * from coursetable where validate='activated' order by coursetitle, coursecode";
$allcourse = mysql_query($querycourse);
$ctr = 0;
$numrows = mysql_num_rows($allcourse);

//column width
$worksheet->setColumn(0, 0, 7);
$worksheet->setColumn(1, 1, 35);
$worksheet->setColumn(2, 2, 15);
$worksheet->setColumn(3, 3, 35);
$worksheet->setColumn(4, 4, 10);
$worksheet->setColumn(5, 5, 20);
$worksheet->setColumn(6, 6, 20);
$worksheet->setColumn(7, 7, 15);
$worksheet->setColumn(8, 8, 8);
$worksheet->setColumn(9, 9, 10);
$worksheet->setColumn(10, 10, 8);
$worksheet->setColumn(11, 11, 11);
$worksheet->setColumn(12, 12, 11);

$worksheet->write(0, 0, 'No', $format_center_bold);
$worksheet->write(0, 1, 'Course Title', $format_center_bold);
$worksheet->write(0, 2, 'Course Code', $format_center_bold);
$worksheet->write(0, 3, 'Course Description', $format_center_bold);
$worksheet->write(0, 4, 'Section', $format_center_bold);
$worksheet->write(0, 5, 'Minimum Student', $format_center_bold);
$worksheet->write(0, 6, 'Maximum Student', $format_center_bold);
$worksheet->write(0, 7, 'Total Student', $format_center_bold);
$worksheet->write(0, 8, 'Room', $format_center_bold);
$worksheet->write(0, 9, 'Semester', $format_center_bold);
$worksheet->write(0, 10, 'Year', $format_center_bold);
$worksheet->write(0, 11, 'Start Time', $format_center_bold);
$worksheet->write(0, 12, 'End Time', $format_center_bold);


for ($x = 1; $x <= $numrows; $x++) {
    $worksheet->write($x, 0, $x, $format_center);
}
$ctr = 1;

while ($row = mysql_fetch_array($allcourse)) {
    $worksheet->write($ctr, 1, $row['coursetitle'], $format_left);
    $worksheet->write($ctr, 2, $row['coursecode'], $format_center);
    $worksheet->write($ctr, 3, $row['description'], $format_left);
    $worksheet->write($ctr, 4, $row['section'], $format_center);
    $worksheet->write($ctr, 5, $row['minnumstud'], $format_center);
    $worksheet->write($ctr, 6, $row['manumstud'], $format_center);
    $worksheet->write($ctr, 7, $row['totalstud'], $format_center);
    $worksheet->write($ctr, 8, $row['room'], $format_center);
    $worksheet->write($ctr, 9, $row['semester'], $format_center);
    $worksheet->write($ctr, 10, $row['year'], $format_center);
    $worksheet->write($ctr, 11, $row['starttime'], $format_center);
    $worksheet->write($ctr, 12, $row['endtime'], $format_center);

    $ctr++;
}


$workbook->close();
?>

